﻿using Sidvall.Collections;

namespace Sidvall.IdentityManager.Data.Model.EntityManagers
{
    public partial class AspNetUsersManager<TEntity, TList>
    {
        #region Public Members

        // Claims
        #region GetClaimValue

        public string GetClaimValue(IAspNetUsers item, string claimType)
        {
            if (item == null)
                return null;
            return this.EntityFacadeManager.GenericEntityFacade.AspNetUserClaimsManager.GetClaimValue(item.AspNetUserClaims, claimType);
        }
        public string GetClaimValue(IAspNetUsers item, string claimType, string defaultValue)
        {
            if (item == null)
                return null;
            return this.EntityFacadeManager.GenericEntityFacade.AspNetUserClaimsManager.GetClaimValue(item.AspNetUserClaims, claimType, defaultValue);
        }

        #endregion
        #region SetClaimValue

        public bool SetClaimValue(IAspNetUsers item, string claimType, string value)
        {
            return SetClaimValue(item, claimType, value, null);
        }
        public bool SetClaimValue(IAspNetUsers item, string claimType, string value, Sidvall.Data.DataRowState? dataRowState)
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                var property = GetOrCreateClaim(item, claimType, dataRowState);
                if (property == null)
                    return false;
                if (property.ClaimValue == value)
                    return false;
                property.ClaimValue = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (!property.DataRowStateIsSelfDirty)
                    property.SetDataRowContext(null, Sidvall.Data.DataRowState.Modified);
                return true;
            }
            else
            {
                return RemoveClaim(item, claimType);
            }
        }

        #endregion
        #region RemoveClaim

        public bool RemoveClaim(IAspNetUsers item, string claimType)
        {
            if (item == null)
                return false;
            if (item.AspNetUserClaims == null)
                return false;
            var property = this.EntityFacadeManager.GenericEntityFacade.AspNetUserClaimsManager.GetClaim(item.AspNetUserClaims, claimType);
            if (property == null)
                return false;
            item.AspNetUserClaims.Remove(property);
            return true;
        }

        #endregion
        #region HasClaim

        public bool HasClaim(IAspNetUsers item, string claimType)
        {
            if (item == null)
                return false;
            return this.EntityFacadeManager.GenericEntityFacade.AspNetUserClaimsManager.HasClaim(item.AspNetUserClaims, claimType);
        }

        #endregion

        #endregion
        #region Private Members

        #region GetOrCreateClaim

        private IAspNetUserClaims GetOrCreateClaim(IAspNetUsers item, string claimType, Sidvall.Data.DataRowState? dataRowState)
        {
            if (item == null)
                return null;
            if (item.AspNetUserClaims == null)
                item.SetAspNetUserClaims((Sidvall.Data.IDataRowProvider<global::Sidvall.IdentityManager.Data.IAspNetUserClaims>)this.EntityFacadeManager.ResourceFactory.CreateItems(Sidvall.IdentityManager.Data.Model.DomainObjects.AspNetUserClaims));
            var claim = this.EntityFacadeManager.GenericEntityFacade.AspNetUserClaimsManager.GetClaim(item.AspNetUserClaims, claimType);
            if (claim == null)
            {
                claim = (IAspNetUserClaims)this.EntityFacadeManager.ResourceFactory.CreateItem(Sidvall.IdentityManager.Data.Model.DomainObjects.AspNetUserClaims, dataRowState);
                claim.UserId = item.Id;
                claim.ClaimType = claimType;
                item.AspNetUserClaims.Add(claim);
            }
            return claim;
        }

        #endregion

        #endregion
    }
}